package com.lib.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class RightNavView extends View {
    private int mWidth;
    private int mHeight;
    private float mTextWidth;
    private Paint mPaint;
    private int mIndex = 0;
    private OnItemSelectedListener mOnItemSelectedListener;
    private static final String[] INDEX = {
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private float mEventX;
    private float mEventY;
    private boolean isOnTouching;

    // 添加padding相关变量
    private int mContentWidth;
    private int mContentHeight;
    private float mContentStartX;

    private float mOY;
    private boolean mShowEffect;

    public void setOnItemSelectedListener(RightNavView.OnItemSelectedListener l) {
        this.mOnItemSelectedListener = l;
    }

    public interface OnItemSelectedListener {
        void onItemSelecting(int index, String indexStr);

        void onItemSelected(int index, String indexStr);
    }

    public RightNavView(Context context) {
        this(context, null);
    }

    public RightNavView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RightNavView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);

        mTextWidth = mPaint.measureText("M");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 考虑padding的测量
        int widthPadding = getPaddingLeft() + getPaddingRight();
        int heightPadding = getPaddingTop() + getPaddingBottom();

        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);

        // 计算实际内容区域
        mContentWidth = mWidth - widthPadding;
        mContentHeight = mHeight - heightPadding;
        mContentStartX = getPaddingLeft();

        float textHeight = mContentHeight / (float) (INDEX.length);
        mPaint.setTextSize(textHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int length = INDEX.length;
        float itemHeight = mContentHeight / (float) length;

        // 考虑padding的绘制位置
        float centerX = mContentStartX + mContentWidth - mTextWidth / 2 - 10;

        // 修复：计算文本基线位置，确保所有字母完整显示
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        float textHeight = fm.descent - fm.ascent;
        float baseYOffset = getPaddingTop() + (itemHeight - textHeight) / 2 - fm.ascent;

        for (int i = 0; i < length; i++) {
            float yPos = baseYOffset + itemHeight * i;

            if (mIndex == i) {
                mPaint.setColor(Color.RED);
                if (isOnTouching) {
                    // 触摸时的偏移位置，考虑padding
                    canvas.drawText(INDEX[i], centerX - 110, yPos, mPaint);
                } else {
                    canvas.drawText(INDEX[i], centerX, yPos, mPaint);
                }
            } else if (isOnTouching) {
                mPaint.setColor(Color.GRAY);
                float offsetX = 0;
                int distance = Math.abs(mIndex - i);

                if (distance == 1) {
                    offsetX = -90;
                } else if (distance == 2) {
                    offsetX = -80;
                } else if (distance == 3) {
                    offsetX = -70;
                } else if (distance == 4) {
                    offsetX = -50;
                } else if (distance == 5) {
                    offsetX = -30;
                }

                if (offsetX != 0) {
                    canvas.drawText(INDEX[i], centerX + offsetX, yPos, mPaint);
                } else {
                    canvas.drawText(INDEX[i], centerX, yPos, mPaint);
                }
            } else {
                mPaint.setColor(Color.GRAY);
                canvas.drawText(INDEX[i], centerX, yPos, mPaint);
            }
        }
    }

    public int getIndex() {
        return mIndex;
    }

    public void setIndex(int index) {
        mIndex = index;
        invalidate();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        int length = INDEX.length;

        // 考虑padding的触摸区域计算
        float touchX = event.getX();
        float touchY = event.getY();

        // 边缘检测：检查是否在View边界内
        boolean isInsideView = touchX >= 0 && touchX <= mWidth
                && touchY >= 0 && touchY <= mHeight;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!isInsideView) {
                    return false;
                }
                mOY = touchY;
            case MotionEvent.ACTION_MOVE:
                mEventX = touchX;
                mEventY = touchY;

                if (Math.abs(mOY - touchY) > 15) {
                    isOnTouching = true;
                }

                if (isInsideView) {// 计算索引时考虑垂直padding
                    float relativeY = mEventY - getPaddingTop();
                    mIndex = (int) (relativeY / (mContentHeight / length));
                    if (mOnItemSelectedListener != null && mIndex >= 0 && mIndex < length) {
                        mOnItemSelectedListener.onItemSelecting(mIndex, INDEX[mIndex]);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                isOnTouching = false;
                if (mOnItemSelectedListener != null && mIndex >= 0 && mIndex < length) {
                    mOnItemSelectedListener.onItemSelected(mIndex, INDEX[mIndex]);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                isOnTouching = false;
                break;
        }
        invalidate();
        return true;
    }
}